package cn.swu.lzr.event;

import cn.swu.lzr.JDBC.DBTools;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.Writer;
import java.util.List;

public class QueryALlServlet extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException
    {

        try{
            DBTools dbTools = new DBTools();



            List<Event> events = dbTools.FindALL();
            ObjectMapper mapper = new ObjectMapper();
            String json = mapper.writeValueAsString(events);


            response.setContentType("application/json");
            response.setCharacterEncoding("UTF-8");

            try (Writer writer =  response.getWriter()){
                writer.write(json);
            }
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }

}
